package screens
{
	import configuration.SourceTextures;
	import flash.display.Loader;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import starling.display.Sprite;
	import starling.display.Image;
	import starling.events.Event;
	//import starling.events.TouchEvent;
	import starling.display.MovieClip;
	import starling.core.Starling;
	import flash.media.*;
	import flash.events.Event;
	import events.Nav;
	
	/**
	 * ...
	 * @author ania
	 */
	
	public class Home extends Screen
	{
				
		private var _texture:Image;
		private var container:MovieClip;

		[Embed(source="../Assets/Animations/activeden-2277362-electrocardiography-ekg.swf")]
		public var ELECTRO:Class;
				
		private var nstage:Stage ;
		private var ligne:flash.display.MovieClip;
				
		private var sonBattement:Sound;
		private var sndChannel:SoundChannel;
		
		//constructor
		public function Home():void
		{
			//super();
			nstage = Starling.current.nativeStage;
			addEventListener(flash.events.Event.ADDED_TO_STAGE, added);
		}
		
		override public function drawContent():void
		{
			super.drawContent();
			
			ligne = new ELECTRO();
			
			//ça ne marche pas :/
			ligne.width = nstage.stageWidth;
			ligne.height = nstage.stageHeight;
			
			//agrandir 
			ligne.scaleX = 2;
			
			//intégrer l'ambiance sonore (battemements de coeur)
			this.setupSound();
						
			nstage.addChild(ligne);
			trace("dans le home");
			nstage.addEventListener(MouseEvent.MOUSE_DOWN, clearElectro);
						
		}
		
		private function clearElectro(e:MouseEvent):void 
		{
			//nstage.removeChild(ligne);
			
			//ligne.visible = false;
			
			//mettre le son a stop
			
			//sndChannel.stop();

			/////////////////changer d'ecran 
			if (Screen.globalScreen == 0)
			{
				this.dispatchEvent(new Nav(Nav.CHANGE_SCREEN, { id:"inApi" }, true));
				trace("--clearElectro fin--");
				++Screen.globalScreen;
			}
			trace("mon test global!"+Screen.globalScreen);
			
		}
	
		//au toucher, soit l'enlever de la scene ou le rendre invisible
		
		public function setupSound():void
		{
			sonBattement = new Sound();
	
			sndChannel = new SoundChannel();
			sonBattement.load(new URLRequest("../src/Assets/sons/heart_beat.mp3"));
			sonBattement.addEventListener(flash.events.Event.COMPLETE, onComplete, false, 0, true);
			
		}
		
		public function onComplete(evt:flash.events.Event):void 
		{
			
			//////////les battements sont lancés et stopés sans le changeView
			//sndChannel = sonBattement.play(0, 99);
			
		}
		
		public function changeView(bo : Boolean): void
		{
			//permet de remetre line.visible a vrai et 
			ligne.visible = bo;
			if (bo==true)
			{
				Screen.globalScreen = 0;
				sndChannel = sonBattement.play(0, 99);
				
				trace(this+"test touchable");
			}
			else 
			{
				sndChannel.stop();
				
			}
		}
	}
}


		
		
	